package com.wellsoft.mobileoa.utils.view;

import java.util.List;

import android.R.integer;
import android.app.AlertDialog;
import android.app.LauncherActivity.ListItem;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageFieldButton;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wmobile.ui.BadgeView;
import com.wmobile.utils.ServiceResponse;

public class JGGView extends BaseView {
	WSOA_PageListItem backListItem = null;

	public JGGView(BaseActivity baseActivity, WSOA_Page page, String url) {
		super(baseActivity);
		this.baseActivity = baseActivity;
		this.page = page;
		this.url = url;

		for (int i = 0; i < page.getPageListSet().getListItems().size(); i++) {
			WSOA_PageListItem tmpListItem = page.getPageListSet()
					.getListItems().get(i);
			WSOA_Action tmpAction = null;
			if (tmpListItem.getAction() != null) {
				tmpAction = tmpListItem.getAction();
			} else {
				List<WSOA_Action> tmpActions = BaseAction.getActionById(
						tmpListItem.getActionID(), page.getActions(), null);
				tmpAction = tmpActions.get(0);
			}
			if (tmpAction.getFuncType().toUpperCase().equals("O")) {
				backListItem = tmpListItem;
				page.getPageListSet().getListItems().remove(i);
				break;
			}
		}
	}

	@Override
	public ViewGroup DrawSelf() {
		if (page.getPageListSet().getListItems() == null) {
			return null;
		}
		Resources resources = baseActivity.getResources();

		selfView = (ViewGroup) LayoutInflater.from(baseActivity).inflate(
				R.layout.jggview, null);

		// 特别设置九宫格的title

		TextView titleTv = (TextView) selfView
				.findViewById(R.id.titleview_titletv);
		TextView titleBackTv = (TextView) selfView
				.findViewById(R.id.titleview_gobacktv);
		Button backBtn = (Button) selfView.findViewById(R.id.titleview_backbtn);
		titleBackTv.setText(backListItem.getMessage());

		titleTv.setText(page.getPageTitle().getMessage());
		backBtn.setOnClickListener(jggBackOnClickListener);
		backBtn.setOnTouchListener(onJggTitleBackTouchListener);
		backBtn.setTag(selfView);

		//

		LinearLayout buttonContainerLl = (LinearLayout) selfView
				.findViewById(R.id.jggview_buttoncontainerll);
		LinearLayout currentHorizontalLl = null;

		WindowManager wm = (WindowManager) baseActivity
				.getSystemService(Context.WINDOW_SERVICE);

		int width = wm.getDefaultDisplay().getWidth();
		int columnWidth = SystemConfig.dip2px(baseActivity, 75);
		int columnSpace = SystemConfig.dip2px(
				baseActivity,
				Float.valueOf(
						baseActivity.getResources().getDimension(
								R.dimen.jggitem_padding)).intValue());
		int columnCount = getColumnCount(width, columnWidth, columnSpace);
		for (int i = 0; i < page.getPageListSet().getListItems().size(); i++) {
			if (i % columnCount == 0) {

				currentHorizontalLl = new LinearLayout(baseActivity);
				buttonContainerLl.addView(currentHorizontalLl);
			}
			View view = (View) LayoutInflater.from(baseActivity).inflate(
					R.layout.jggviewitem, null);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					columnWidth, columnWidth);
			params.setMargins(columnSpace / 2, columnSpace / 2,
					columnSpace / 2, columnSpace / 2);

			view.setLayoutParams(params);

			TextView textView = (TextView) view
					.findViewById(R.id.jggviewitem_funcnametv);
			ImageView jggButton = (ImageView) view
					.findViewById(R.id.jggviewitem_funcibtn);

			textView.setText(page.getPageListSet().getListItems().get(i)
					.getMessage());

			int indentify = resources.getIdentifier(
					baseActivity.getPackageName()
							+ ":drawable/"
							+ "jggview_"
							+ page.getPageListSet().getListItems().get(i)
									.getImg(), null, null);
			if (indentify > 0) {
				jggButton.setImageResource(indentify);
			}

			view.setTag(i);

			view.setOnClickListener(onColumnClickedListener);
			view.setOnTouchListener(onIconTouchListener);

			currentHorizontalLl.addView(view);

			// badge
			if (SystemConfig.isInt(page.getPageListSet().getListItems().get(i)
					.getCount())) {
				int count = Integer.parseInt(page.getPageListSet()
						.getListItems().get(i).getCount());
				if (count == 0) {

				} else if (count > 99) {
					BadgeView badge = new BadgeView(baseActivity, view);
					badge.setText("99");
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.toggle();
				} else {
					BadgeView badge = new BadgeView(baseActivity, view);
					badge.setText(page.getPageListSet().getListItems().get(i)
							.getCount());
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.toggle();
				}
			}
		}
		return selfView;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			jggBackOnClickListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	protected OnTouchListener onJggTitleBackTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			View titleView = (View) v.getTag();
			ImageView iv = (ImageView) titleView
					.findViewById(R.id.titleview_backiv);
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				iv.setBackgroundColor(Color.argb(63, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				iv.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				iv.setBackgroundColor(Color.argb(0, 255, 255, 255));
			} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
				iv.setBackgroundColor(Color.argb(0, 255, 255, 255));
			}
			return false;
		}
	};

	public int getColumnCount(int screeenWidth, int columnWidth, int marginWidth) {
		int columnCount;
		columnCount = (screeenWidth - marginWidth)
				/ (columnWidth + marginWidth);
		return columnCount;
	}

	OnClickListener onColumnClickedListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			synchronized (JGGView.this.isExecuting) {
				if (isExecuting) {
					return;
				}
				isExecuting = true;
				AlertDialog alertDialog = baseActivity.showWaitingDialog();
				int index = Integer.parseInt(v.getTag().toString());
				WSOA_PageListItem listItem = page.getPageListSet()
						.getListItems().get(index);
				ServiceResponse response = BaseAction.executeActions(
						listItem.getActionID(), listItem.getAction(),
						JGGView.this);
				alertDialog.dismiss();
				isExecuting = false;
				if (!response.getExeFlag()) {
					baseActivity.showMessage(response.getMessage());
					return;
				}
			}
		}
	};

	OnTouchListener onIconTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int index = Integer.parseInt(v.getTag().toString());
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				onTouchDown(index, v);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				onTouchUp(index, v);
			} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
				onTouchUp(index, v);
			} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
				onTouchUp(index, v);
			}
			return false;
		}
	};

	public void onTouchDown(int index, View v) {
		TextView textView = (TextView) v
				.findViewById(R.id.jggviewitem_funcnametv);
		ImageView jggButton = (ImageView) v
				.findViewById(R.id.jggviewitem_funcibtn);
		int indentify = baseActivity.getResources().getIdentifier(
				baseActivity.getPackageName()
						+ ":drawable/"
						+ "jggview_"
						+ page.getPageListSet().getListItems().get(index)
								.getImg() + "pressed", null, null);
		if (indentify > 0) {
			jggButton.setImageResource(indentify);
		}
		textView.setTextColor(Color.WHITE);
		v.setBackgroundColor(Color.argb(255, 90, 171, 216));
	}

	public void onTouchUp(int index, View v) {
		TextView textView = (TextView) v
				.findViewById(R.id.jggviewitem_funcnametv);
		ImageView jggButton = (ImageView) v
				.findViewById(R.id.jggviewitem_funcibtn);
		int indentify = baseActivity.getResources().getIdentifier(
				baseActivity.getPackageName()
						+ ":drawable/"
						+ "jggview_"
						+ page.getPageListSet().getListItems().get(index)
								.getImg(), null, null);
		if (indentify > 0) {
			jggButton.setImageResource(indentify);
		}
		textView.setTextColor(Color.argb(255, 47, 159, 207));
		v.setBackgroundColor(Color.argb(255, 233, 248, 255));
	}

	OnClickListener jggBackOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			synchronized (JGGView.this.isExecuting) {
				if (isExecuting) {
					return;
				}
				isExecuting = true;
				AlertDialog alertDialog = baseActivity.showWaitingDialog();
				ServiceResponse response = BaseAction.executeActions(
						backListItem.getActionID(), backListItem.getAction(),
						JGGView.this);
				alertDialog.dismiss();
				isExecuting = false;
				if (!response.getExeFlag()) {
					baseActivity.showMessage(response.getMessage());
					return;
				}
			}
		}
	};
}
